<template>
  <div class="sdlist">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" style="margin-bottom:8vh;" >
      <van-list v-model="loading" :finished="finished" finished-text="已加载全部数据" @load="onLoad">
        <van-checkbox-group v-model="selecteddata" ref="checkboxGroup" >
          <van-cell  v-for="(item, index) in list" :key="item.id" :value="item.value" @click="toggle(index)">
            <template #title>
              <span>{{ item.title }}</span><br>
              <span style="font-size: 12px;color: #969799;" v-html="item.label"></span>
            </template>
            <van-checkbox :name="item" ref="checkboxes" slot="right-icon" />
          </van-cell>
        </van-checkbox-group>
      </van-list>
    </van-pull-refresh>
    <van-tabbar v-model="active" safe-area-inset-bottom >
      <van-tabbar-item @click="openSDNew">确定</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup, Icon, SwipeCell, Dialog, Checkbox, CheckboxGroup } from 'vant'
import qs from 'qs'
Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(Icon).use(SwipeCell).use(Dialog).use(Checkbox).use(CheckboxGroup)
export default {
  name: 'list',
  data () {
    return {
      idobject: this.$route.query.idobject,
      sdtype: this.$route.query.sdtype,
      subviewid: this.$route.query.subviewid,
      subrefobject: this.$route.query.subrefobject,
      relopeid: this.$route.query.relopeid,
      headid: this.$route.query.selectedid,
      refobject: this.$route.query.refobject,
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      active: 0,
      startline: 1,
      pagecount: 50,
      selecteddata: []
    }
  },
  methods: {
    onLoad () {
      let data = {
        'startline': this.startline,
        'pagecount': this.pagecount,
        'sdtype': this.sdtype,
        'subviewid': this.subviewid,
        'subrefobject': this.subrefobject,
        'relopeid': this.relopeid,
        'headid': this.headid
      }
      this.axios({
        method: 'post',
        url: '/api/' + this.idobject + '.sdlistsub.do',
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          let rsdata = res.data.jsondata.fieldValue
          for (let i = 0; i < rsdata.length; i++) {
            this.list.push(rsdata[i])
          }
          if (rsdata == null || rsdata.length === 0 || rsdata.length < this.pagecount) {
            this.loading = false
            this.finished = true
          } else {
            this.startline = this.startline + rsdata.length
            this.loading = false
          }
        }
      }).catch(res => {
        this.reserror(res)
      })
    },
    onRefresh () {
      this.list = []
      this.startline = 1
      this.finished = false
      this.loading = true
      this.isLoading = false
      this.onLoad()
    },
    toggle (index) {
      this.$refs.checkboxes[index].toggle()
    },
    openSDNew () {
      let bodyid = ''
      for (let i = 0; i < this.selecteddata.length; i++) {
        bodyid += this.selecteddata[i].id + ';'
      }
      this.$router.push({ name: 'sdnew', query: { idobject: this.idobject, refobject: this.refobject, relopeid: this.relopeid, headid: this.headid, bodyid: bodyid, sdtype: this.$route.query.sdtype, viewid: this.$route.query.viewid, subviewid: this.$route.query.subviewid, subrefobject: this.$route.query.subrefobject } })
    }
  }
}
</script>
